package com.ruoyi.system.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.EduRegion;
import com.ruoyi.system.service.IEduRegionService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.Ztree;

/**
 * 地区Controller
 * 
 * @author ruoyi
 * @date 2024-03-04
 */
@Controller
@RequestMapping("/system/region")
public class EduRegionController extends BaseController
{
    private String prefix = "system/region";

    @Autowired
    private IEduRegionService eduRegionService;

    @RequiresPermissions("system:region:view")
    @GetMapping()
    public String region()
    {
        return prefix + "/region";
    }

    /**
     * 查询地区树列表
     */
    @RequiresPermissions("system:region:list")
    @PostMapping("/list")
    @ResponseBody
    public List<EduRegion> list(EduRegion eduRegion)
    {
        List<EduRegion> list = eduRegionService.selectEduRegionList(eduRegion);
        return list;
    }

    /**
     * 导出地区列表
     */
    @RequiresPermissions("system:region:export")
    @Log(title = "地区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EduRegion eduRegion)
    {
        List<EduRegion> list = eduRegionService.selectEduRegionList(eduRegion);
        ExcelUtil<EduRegion> util = new ExcelUtil<EduRegion>(EduRegion.class);
        return util.exportExcel(list, "地区数据");
    }

    /**
     * 新增地区
     */
    @GetMapping(value = { "/add/{regionId}", "/add/" })
    public String add(@PathVariable(value = "regionId", required = false) Long regionId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(regionId))
        {
            mmap.put("eduRegion", eduRegionService.selectEduRegionByRegionId(regionId));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存地区
     */
    @RequiresPermissions("system:region:add")
    @Log(title = "地区", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(EduRegion eduRegion)
    {
        return toAjax(eduRegionService.insertEduRegion(eduRegion));
    }

    /**
     * 修改地区
     */
    @RequiresPermissions("system:region:edit")
    @GetMapping("/edit/{regionId}")
    public String edit(@PathVariable("regionId") Long regionId, ModelMap mmap)
    {
        EduRegion eduRegion = eduRegionService.selectEduRegionByRegionId(regionId);
        mmap.put("eduRegion", eduRegion);
        return prefix + "/edit";
    }

    /**
     * 修改保存地区
     */
    @RequiresPermissions("system:region:edit")
    @Log(title = "地区", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EduRegion eduRegion)
    {
        return toAjax(eduRegionService.updateEduRegion(eduRegion));
    }

    /**
     * 删除
     */
    @RequiresPermissions("system:region:remove")
    @Log(title = "地区", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{regionId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("regionId") Long regionId)
    {
        return toAjax(eduRegionService.deleteEduRegionByRegionId(regionId));
    }

    /**
     * 选择地区树
     */
    @GetMapping(value = { "/selectRegionTree/{regionId}", "/selectRegionTree/" })
    public String selectRegionTree(@PathVariable(value = "regionId", required = false) Long regionId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(regionId))
        {
            mmap.put("eduRegion", eduRegionService.selectEduRegionByRegionId(regionId));
        }
        return prefix + "/tree";
    }

    /**
     * 加载地区树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = eduRegionService.selectEduRegionTree();
        return ztrees;
    }
}
